package com.back_end.NetWork.UDP_DEMO;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

//接收端，UDP不需要服务端，此处为模拟
public class UdpServerDemo01 {

    public static void main(String[] args) throws Exception {
        //开放端口
        DatagramSocket socket = new DatagramSocket(9090);

        //接收数据包
        byte[] buffer = new byte[1024]; //得有空间
        DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length); //接收包

        socket.receive(packet); //阻塞接收，相当于活着，活着才能随时接收包裹

        System.out.println(new String(packet.getData(),0, packet.getLength()));
        System.out.println(packet.getAddress().getHostAddress());

        //4.关闭流
        socket.close();


    }
}
